package com.example.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.reggie.bean.Dish;
import com.example.reggie.bean.DishFlavor;
import com.example.reggie.dto.DishDto;
import com.example.reggie.service.DishFlavorService;
import com.example.reggie.service.DishService;
import com.example.reggie.utils.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @Transactional
    @PostMapping
    public R<String> add(@RequestBody DishDto dishDto){
        //保存dish
        dishService.save(dishDto);
        //添加flavors
        List<DishFlavor> dishFlavors=dishDto.getFlavors();
        dishFlavors=(ArrayList<DishFlavor>) dishFlavors.stream().map(item -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(dishFlavors);
        return R.success("添加菜单成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        Page<Dish> pageInfo=new Page<>(page,pageSize);
        LambdaQueryWrapper<Dish> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(name==null?false:true,Dish::getName,name);
        dishService.page(pageInfo,lambdaQueryWrapper);
        return R.success(pageInfo);
    }

    @DeleteMapping
    public R<String> remove(String[] ids){
        List<String> idsList= Arrays.asList(ids);
        for (String id:ids) {
            //删除口味
            LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper=new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(DishFlavor::getDishId,Long.parseLong(id));
            dishFlavorService.remove(lambdaQueryWrapper);

        }
        dishService.removeByIds(idsList);
        return R.success("删除成功");
    }
    @Transactional
    @GetMapping("/{id}")
    public R<DishDto> findOne(@PathVariable Long id){
        Dish dish =dishService.getById(id);
        DishDto dishDto=new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DishFlavor::getDishId,id);
        List<DishFlavor> dishFlavorList = dishFlavorService.list(lambdaQueryWrapper);
        dishDto.setFlavors(dishFlavorList);
        return R.success(dishDto);
    }

    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        //清理flavors表
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper =new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(dishFlavorLambdaQueryWrapper);
        //更新flavors表
        dishFlavorService.saveBatch(dishDto.getFlavors());
        //更新dish表
        Dish dish=(Dish) dishDto;
        dishService.updateById(dish);
        return R.success("修改菜单成功");
    }

    @PostMapping("/status/{type}")
    @Transactional
    public R<String> status(@PathVariable int type,Long[] ids){
        List<Long> idsList=new ArrayList<>();
        for (Long id:ids) {
            idsList.add(id);
        }
        List<Dish> dishList = dishService.listByIds(idsList);
        dishList.forEach(dish -> {
            dish.setStatus(type);
        });
        dishService.updateBatchById(dishList);
        return R.success("修改状态成功");
    }

    @GetMapping("/list")
    public R<List<DishDto>> list(Long categoryId){
        LambdaQueryWrapper<Dish> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dish::getCategoryId,categoryId);
        List<Dish> dishes = dishService.list(lambdaQueryWrapper);
        List<DishDto> list=dishes.stream().map(item->{
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item,dishDto);
            LambdaQueryWrapper<DishFlavor> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(DishFlavor::getDishId,item.getId());
            List<DishFlavor> dishFlavorList=dishFlavorService.list(queryWrapper);
            dishDto.setFlavors(dishFlavorList);
            return dishDto;
        }).collect(Collectors.toList());
        return R.success(list);
    }
}
